package com.example.richobject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.richobject.recyc_Adaper.RecyclerViewAdapter;
import com.example.richobject.recyc_Adaper.RecyclerViewHolder;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import static com.example.richobject.R.layout.item_goods_detail;

/**
 * MAC:     192.17.0.7  8080
 * Android: 192.17.0.63 50394
 */
public class MainActivity extends BaseActivity {

    private RecyclerView rlModule;
    private RecyclerView rlModuleSecond;
    private List<GoodsDetailBean> mDataFirst = new ArrayList<>();
    private List<GoodsDetailBean> mDataSecond = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rlModule = findViewById(R.id.rlModule);
        rlModuleSecond = findViewById(R.id.rlModuleSecond);
        initView();
    }

    private void initView() {
        mDataFirst.add(new GoodsDetailBean("booba汁","38元",R.drawable.img_module_goods_one,15));
        mDataFirst.add(new GoodsDetailBean("勿忘我","22元",R.drawable.img_module_goods_two,15));
        mDataFirst.add(new GoodsDetailBean("秘密","18元",R.drawable.img_module_goods_three,15));
        mDataFirst.add(new GoodsDetailBean("热恋","29元",R.drawable.img_module_goods_four,15));
        mDataFirst.add(new GoodsDetailBean("盛夏","19元",R.drawable.img_module_goods_five,15));

        mDataSecond.add(new GoodsDetailBean("天涯海角","23元",R.drawable.img_module_goods_six,15));
        mDataSecond.add(new GoodsDetailBean("雪上飞花","26元",R.drawable.img_module_goods_sevent,15));
        mDataSecond.add(new GoodsDetailBean("幽静","35元",R.drawable.img_module_goods_eight,15));
        mDataSecond.add(new GoodsDetailBean("心喜","27元",R.drawable.img_module_goods_ninght,15));
        mDataSecond.add(new GoodsDetailBean("lucky汁","50元",R.drawable.img_module_goods_ten,15));

        LinearLayoutManager linearLayoutManagerFirst = new LinearLayoutManager(this);
        linearLayoutManagerFirst.setOrientation(RecyclerView.HORIZONTAL);
        rlModule.setLayoutManager(linearLayoutManagerFirst);

        LinearLayoutManager linearLayoutManagerSecond = new LinearLayoutManager(this);
        linearLayoutManagerSecond.setOrientation(RecyclerView.HORIZONTAL);
        rlModuleSecond.setLayoutManager(linearLayoutManagerSecond);

        RecyclerViewAdapter<GoodsDetailBean> adapter = new RecyclerViewAdapter<GoodsDetailBean>(MainActivity.this,
                item_goods_detail, mDataFirst) {
            @Override
            protected void convert(RecyclerViewHolder holder, final GoodsDetailBean dataBean, final int position) throws UnsupportedEncodingException {
                ImageView ivImg = holder.getView(R.id.ivModule);

                //图片赋值
                Glide.with(MainActivity.this)
                        .load(dataBean.getGoodsRes())
                        .priority(Priority.LOW)
                        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                        .skipMemoryCache(true)
                        .into(ivImg);

                //图片点击事件监听
                ivImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dataBean.getGoodsANum() > 0) {
                            dataBean.setGoodsANum(dataBean.getGoodsANum() - 1);
                            sendControl("192.17.0.7", "8080", position + 1);
                        }else{
                            showToast("库存不足！");
                        }
                    }
                });

                //展品名称、价格、单位赋值
                holder.setText(R.id.tvModuleName, dataBean.getGoodsName()+ dataBean.getGoodsPrice() + "/杯");
            }
        };

        rlModule.setAdapter(adapter);


        RecyclerViewAdapter<GoodsDetailBean> adapterSecond = new RecyclerViewAdapter<GoodsDetailBean>(MainActivity.this,
                item_goods_detail, mDataSecond) {
            @Override
            protected void convert(RecyclerViewHolder holder, final GoodsDetailBean dataBean, final int position) throws UnsupportedEncodingException {
                ImageView ivImg = holder.getView(R.id.ivModule);

                //图片赋值
                Glide.with(MainActivity.this)
                        .load(dataBean.getGoodsRes())
                        .priority(Priority.LOW)
                        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                        .skipMemoryCache(true)
                        .into(ivImg);

                //图片点击事件监听
                ivImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dataBean.getGoodsANum() > 0) {
                            dataBean.setGoodsANum(dataBean.getGoodsANum() - 1);
                            sendControl("192.17.0.7", "8080", position + 6);
                        }else{
                            showToast("库存不足！");
                        }
                    }
                });

                //展品名称、价格、单位赋值
                holder.setText(R.id.tvModuleName, dataBean.getGoodsName()+ dataBean.getGoodsPrice() + "/杯");
            }
        };
        rlModuleSecond.setAdapter(adapterSecond);
    }
}
